package com.web.activiti.service.Impl;

import com.fasterxml.jackson.core.JsonFactory;
import com.web.activiti.mapper.*;
import com.web.activiti.pojo.*;
import com.web.activiti.service.SysService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.UUID;

@Service
public class SysServiceImpl implements SysService {
    @Autowired
    private SysPermissionCustomMapper sysPermissionCustomMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public List<TreeMenu> loadTreeMenu () {
        return sysPermissionCustomMapper.findMenuList();
    }

    @Override
    public List<SysPermission> findPermissionListByUserId (String id) {
        return sysPermissionCustomMapper.findPermissionListByUserId(id);
//        return null;
    }

    @Override
    public List<SysRole> findAllRole () {
        return sysRoleMapper.selectByExample(new SysRoleExample());
    }

    //获取耳机管理人员
    @Override
    public List<SysUserRole> getUserAndRoleList () {
        SysUserRoleExample example = new SysUserRoleExample();
        SysUserRoleExample.Criteria criteria = example.createCriteria();
//        criteria.andSysUserIdLike("%经理%");
        List<SysUserRole> sysUserRoles = null;
        try {
            sysUserRoles = sysUserRoleMapper.selectByExample(example);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sysUserRoles;
    }

    @Override
    public String addUser (Employee employee) {
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setId(employee.getId().toString());
        sysUserRole.setSysUserId(employee.getName());
        sysUserRole.setSysRoleId(employee.getRole());
        String msg="添加成功！";
        sysUserRoleMapper.insert(sysUserRole);
        System.out.println(employee.getId());
        return msg;
    }

    @Override
    public SysRole findRoleByRoleId (String roleId) {
        return sysRoleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public void delRoleById (String roleId) {
        sysRoleMapper.delRoleById(roleId);
    }

    @Override
    public void reRoleById (String roleId) {
        sysRoleMapper.reRoleById(roleId);
    }

    @Override
    public void updateRoleAndPermission (String roleId,String[] permissionIds) {
        SysRolePermissionExample example = new SysRolePermissionExample();
        SysRolePermissionExample.Criteria criteria = example.createCriteria();
        criteria.andSysRoleIdEqualTo(roleId);
        sysRolePermissionMapper.deleteByExample(example);
        for (String permissionId : permissionIds) {
            SysRolePermission roleperMission = new SysRolePermission();
            String uid= UUID.randomUUID().toString();
            roleperMission.setId(uid);
            roleperMission.setSysPermissionId(permissionId);
            roleperMission.setSysRoleId(roleId);
            sysRolePermissionMapper.insert(roleperMission);
        }
    }

    @Override
    public void addRole (SysPermission permission) {
        if (permission.getAvailable()==null&&!permission.getAvailable().equals("1")){
            permission.setAvailable("0");
        }
        sysPermissionMapper.insert(permission);
    }

    @Override
    public void addRoleAndPermission (String name,String[] permissionIds) {
        SysRole role = new SysRole();
        String uid=UUID.randomUUID().toString();
        role.setId(uid);
        role.setName(name);
        role.setAvailable("1");
        sysRoleMapper.insert(role);

        //为新角色关联权限
        SysRolePermission rp = new SysRolePermission();
        for (String pid : permissionIds) {
            String rpuid=UUID.randomUUID().toString();
            rp.setId(rpuid);
            rp.setSysRoleId(uid);
            rp.setSysPermissionId(pid);

            sysRolePermissionMapper.insert(rp);
        }
    }
}
